package rhinestone.TUI;

import java.util.Scanner;
import java.util.InputMismatchException;

import rhinestone.Model.Staff;

/**
 * The class which holds references to all UIs through the beauty of polymorphism.
 *
 * @author Group 2
 * @version 2010.12.15
 */
public class MainMenuUI {
	private final static String CPR_MESSAGE = "Employee CPR: ";
	private final static String PSW_MESSAGE = "Password: ";
	public static Staff LOGGED_IN;
	private UI saleUI;
	private UI productUI;
	private UI leasingUI;
    private UI staffUI;
    private UI orderUI;
	private UI customerUI;
	private UI statisticUI;

	/**
	 * Controller initializes all the UIs and pass a controller to them.
	 */
	public MainMenuUI(UI saleUI, UI productUI, UI leasingUI, UI staffUI, UI orderUI, UI customerUI, UI statisticUI) {
		this.saleUI = saleUI;
		this.productUI = productUI;
		this.leasingUI = leasingUI;
        this.staffUI = staffUI;
        this.orderUI = orderUI;
		this.customerUI = customerUI;
		this.statisticUI = statisticUI;
	}

	/**
	 * The logic of the UI.
	 * Depending on the users choice, it calls a particular UI.
	 */
	public void startMenu() {
		promptLogin();
		boolean finished = false;
		while(!finished) {
			printMenu();
			int choice = promptChoiceInput();
			switch(choice) {
				case 1:
					moveToSale();
					break;
				case 2:
					moveToCrudProducts();
					break;
				case 3:
					moveToLeasing();
					break;
				case 4:
					moveToStatistics();
					break;
                case 5:
                    moveToStaff();
                    break;
				case 6:
					moveToOrders();
					break;
				case 7:
					moveToCrudCustomers();
					break;
				case 8:
					finished = true;
					break;
				default:
					break;
			}
		}
	}

	/**
	 * Prints a menu the user should see.
	 */
	protected void printMenu() {
		System.out.println(" ----- MAIN MENU ----- ");
		System.out.println("\t(-1-) Checkout products");
		System.out.println("\t(-2-) CRUD products");
		System.out.println("\t(-3-) Leasing services");
		System.out.println("\t(-4-) Statistics");
        System.out.println("\t(-5-) Staff");
		System.out.println("\t(-6-) Orders");
		System.out.println("\t(-7-) CRUD customers");
		System.out.println("\t(-8-) Exit");
	}

	/**
	 * Prompts the user to take a choice between menu options.
	 * @return A choice the user have chosen.
	 */
	protected int promptChoiceInput() {
		System.out.print("Your choice: ");
		int choice = 0;
		try {
			choice = new Scanner(System.in).nextInt();
		} catch(InputMismatchException ex) {
			System.err.println("Integers allowed only.");
			System.err.println("Try again.");
			promptChoiceInput();
		}
		return choice;
	}

	/**
	 * Down there are a methods which are called by startMenu method
	 * depending on the users choice. They call a particular UI.
	 */
	private void moveToSale() {
		saleUI.startMenu();
	}
	private void moveToCrudProducts() {
		productUI.startMenu();
	}
	private void moveToLeasing() {
		leasingUI.startMenu();
	}
    private void moveToStaff(){
		if(LOGGED_IN.getRank() >= Staff.MANAGER_RANK)
			staffUI.startMenu();
		else {
			System.err.println("Permission denied.");
			System.err.println("You are not authorized to go there.");
		}
    }
	private void moveToStatistics() {
		if(LOGGED_IN.getRank() >= Staff.MANAGER_RANK)
			statisticUI.startMenu();
		else {
			System.err.println("Permission denied.");
			System.err.println("You are not authorized to go there.");
		}
	}
	private void moveToOrders() {
		orderUI.startMenu();
	}
	private void moveToCrudCustomers() {
		customerUI.startMenu();
	}

	private void promptLogin() {
		boolean finished = false;
		while(!finished) {
			System.out.println(" ----- LOGIN WINDOW -----");
			String cpr = promptStringInput(CPR_MESSAGE);
			String psw = promptStringInput(PSW_MESSAGE);
			Staff staff = staffUI.validateEmployee(cpr, psw);
			if(staff != null) {
				LOGGED_IN = staff;
				finished = true;
			}
			else
				System.err.println("Wrong login.");
		}
	}

	private String promptStringInput(String message) {
		System.out.print(message);
		String input = new Scanner(System.in).nextLine();
		return input;
	}
}
